package com.lagou.servlet;

import com.lagou.entity.Student;
import com.lagou.service.StudentService;
import com.lagou.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "StudentQueryServlet" , urlPatterns = "/studentQueryServlet")
public class StudentQueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码方式， 解决中文乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");

        // 1、获取前台得到的ids数据内容
        String idStr = request.getQueryString();
        idStr = idStr.replace("id=","");
        int id = Integer.parseInt(idStr);

        // 2、通过Service层实现对应功能
        StudentService studentService =  new StudentServiceImpl();
        Student findStudent = studentService.studentFindService(id);
        System.out.println(findStudent);
        List<Student> studentList = new ArrayList<>();
        studentList.add(findStudent);
        request.getSession().setAttribute("studentList", studentList);
        response.sendRedirect("manageStudent.jsp");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
